package com.spectos.m42.blackberry.screen;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.CheckboxField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.PasswordEditField;
import net.rim.device.api.ui.component.TextField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

public class LoginScreen extends MainScreen {
	
	private final static String LOGO_PATH = "images/spectos.png";
	
	private HorizontalFieldManager topManager; //contain logo
	private HorizontalFieldManager bottomManager;//Contain Login button and Cancel button
	private HorizontalFieldManager middleManager; //Contain input form
	
	private BitmapField logoField;
	private Bitmap logoImage;
	
	private LabelField usernameLabel, passwordLabel;
	private TextField usernameInput;
	private PasswordEditField passwordInput;
	private CheckboxField rememberCheckbox;
	
	private ButtonField loginButton, cancelButton;
	
	private FieldChangeListener loginListener;
	public LoginScreen(){
		super();
		init();
			
		add(topManager);
		add(middleManager);
		add(bottomManager);
	}
	private void init(){
		topManager = new HorizontalFieldManager(HorizontalFieldManager.FIELD_HCENTER | HorizontalFieldManager.FIELD_VCENTER);
		topManager.setPadding(10, 10, 10, 10);
		bottomManager = new HorizontalFieldManager(HorizontalFieldManager.FIELD_HCENTER | HorizontalFieldManager.FIELD_VCENTER);
		bottomManager.setPadding(10, 10, 10, 10);
		middleManager = new HorizontalFieldManager();
		middleManager.setMargin(10, 10, 10, 10);
		logoField = new BitmapField();
		logoImage = Bitmap.getBitmapResource(LOGO_PATH);
		logoField.setBitmap(logoImage);
		
		usernameLabel = new LabelField("Username:", FIELD_RIGHT);
		passwordLabel = new LabelField("Password:", FIELD_RIGHT);
		usernameInput = new TextField(TextField.EDITABLE | TextField.FIELD_LEFT);		
		passwordInput = new PasswordEditField();
		rememberCheckbox = new CheckboxField("Remember me", false);
		
		loginButton = new ButtonField("Login", Field.FOCUSABLE);
		cancelButton = new ButtonField("Cancel", Field.FOCUSABLE);
		loginListener = new FieldChangeListener(){

			public void fieldChanged(Field field, int context) {
				if(field.equals(loginButton)){
					login();
				} else if(field.equals(cancelButton)){
					System.exit(0);
				}
			}
			
		};
		loginButton.setChangeListener(loginListener);
		cancelButton.setChangeListener(loginListener);
		
		
		topManager.add(logoField);
		
		bottomManager.add(loginButton);		
		bottomManager.add(cancelButton);
		
		
		VerticalFieldManager column1= new VerticalFieldManager(VerticalFieldManager.FIELD_RIGHT);
		VerticalFieldManager column2 = new VerticalFieldManager(VerticalFieldManager.FIELD_LEFT);
		column1.add(usernameLabel);
		column1.add(passwordLabel);
		column2.add(usernameInput);
		column2.add(passwordInput);
		column2.add(rememberCheckbox);
		middleManager.add(column1);
		middleManager.add(column2);
	}
	public void login(){
		UiApplication theApp = UiApplication.getUiApplication();
		theApp.popScreen(theApp.getActiveScreen());
		theApp.pushScreen(new InboxScreen());
	}
}
